starter.controller('loginController', function($rootScope,$state,$ionicHistory,$ionicPopup,$timeout,$scope,Page,localStore,$location,$interval, ApiEndpoint, settings) {
	
	$scope.login = {
			captchaUrl : settings.imgCaptchaUrl + '?appId=' + settings.appId + '&_d=' + Math.random(),
			mobile : '',
			catpcha : '',
			vertifiCode : ''
	};
	
	
	$("#mobile").attr("readonly",false); 
	$("#catpcha").attr("readonly",false); 
	$("#vertifiCode").attr("readonly",true); 
	$("#loginBtn").attr("disabled",true); 

	$scope.vertifiErrorMsg = "";
	$scope.vertifiCodeTime = 0;
	
	$("#mobile").focus(function(){
		$scope.vertifiErrorMsg = "";
    });
	
	$("#vertifiCode").focus(function(){
		$scope.vertifiErrorMsg = "";
    });
	
	$("#captcha").focus(function(){
		$scope.vertifiErrorMsg = "";
    });
	
	
	$scope.updateCaptcha = function(){
		$scope.login.captchaUrl = settings.imgCaptchaUrl + '?appId=' + settings.appId + '&_d=' + Math.random();
	};
	
	$scope.valdateMobile = function(mobile){
		var reg = /^1[3|4|5|7|8]\d{9}$/;
		if(reg.test(mobile)){
			$("#vertifiCode").attr("readonly",false); 
			return true;
		}else{
			$scope.vertifiErrorMsg = "请输入有效的手机号码！";
			return false;
		}
	};
	
	$scope.valdateCaptcha = function(captcha){
		var reg = /^[A-Za-z0-9]{4}$/;
		if(reg.test(captcha)){
			return true;
		}else{
			$scope.vertifiErrorMsg = "请输入有效图形验证码！";
			return false;
		}
	};
	
	$scope.valdateVertifiCode = function(vertifiCode){
		var reg = /^\d{6}$/;
		if(reg.test(vertifiCode)){
			$("#loginBtn").attr("disabled",false); 
			return true;
		}else{
			$scope.vertifiErrorMsg = "请输入有效短信验证码！";
			return false;
		}
	};
	
	$scope.sendVertifiCode = function(){
		if($scope.valdateMobile($scope.login.mobile) && $scope.valdateCaptcha($scope.login.captcha)){
			$("#mobile").attr("readonly",true); 
			$("#catpcha").attr("readonly",true);
			var param = {
				appId: settings.appId,
				phone: $scope.login.mobile,
				captcha : $scope.login.captcha
			};
			Page.ajaxPostQuiet(settings.smsCaptchaUrl, param, function(resp) {
				$("#vertifiCode").attr("readonly",false); 
				$("#loginBtn").attr("disabled",false); 
				Page.notice('验证码已发送');
         	  	$scope.vertifiCodeTime = 60;
				$scope.vertifiCodeTimer = $interval(function(){
	                $scope.vertifiCodeTime -- ;
	                if($scope.vertifiCodeTime <= 0){
	                	$interval.cancel($scope.vertifiCodeTimer);
	                	$("#mobile").attr("readonly",false);
	                	$("#catpcha").attr("readonly",false);
	                }
	            },1000,60);
        	},function(resp){
				$("#mobile").attr("readonly",false); 
				$("#catpcha").attr("readonly",false);
				$("#vertifiCode").attr("readonly",true); 
				$("#loginBtn").attr("disabled",true); 
				$scope.vertifiErrorMsg = resp.resultDes;
        	});
		}
	};

	$scope.wxlogin = function () {
		if($scope.valdateMobile($scope.login.mobile) && $scope.valdateVertifiCode($scope.login.vertifiCode)){
			$("#loginBtn").attr("disabled",true); 
			$("#vertifiCode").attr("readonly",true); 
			var param = {
					openId : $rootScope.user.openId,
					phone: $scope.login.mobile,
					captcha : $scope.login.vertifiCode
			};
			Page.ajaxPostQuiet('wx/register.do', param, function(resp) {
				$rootScope.user = resp.data;
				Page.notice('登陆成功!',function(){
					$scope.goHome();
				});
			},function(resp){
				$scope.vertifiErrorMsg = resp.resultDes;
				$("#mobile").attr("readonly",false);
				$("#catpcha").attr("readonly",false);
				$("#vertifiCode").attr("readonly",false);
				$scope.updateCaptcha();
			});
		}
    };
});